Interactive maps with R
Given a whole semesters’ worth of data viz know-how, how would you assess a data visualization? Which principles and practices would you want to see employed?
We’ve come up with a starting point:
In small groups in a breakout room, please discuss 3-5 things that you would like to either change or modify. You can make suggestions related to the Major Categories, Expectations, or Point Value.
Discuss for five minutes and return to the whole group with a plan for how to share.
For next week, you will need to develop up to three slides. Here are the requirements:
You can use Google Slides, Powerpoint, Keynote, or another tool.
To ensure you receive full credit, please upload or share a link to your presentation by 9:00 am on Wednesday, May 1 for the Final Project assignment:
https://utk.instructure.com/courses/196893/assignments/1779345
As with most design-related things, it depends on the goals and the audience of your visualization.

Other packages include:
The rgbif package accesses data from the Global Biodiversity Information Facility (GBIF).
Let’s say we want to download data on the Monarch butterfly 🦋
dan_ple <-
rgbif::occ_data(scientificName = 'Danaus plexippus')
dan_ple_dat <- dan_ple$data
glimpse(dan_ple_dat)Rows: 500
Columns: 83
$ key <chr> "4507686934", "4507704024", "4507984043"…
$ scientificName <chr> "Danaus plexippus (Linnaeus, 1758)", "Da…
$ decimalLatitude <dbl> -34.90042, -34.86286, -26.45418, 28.4724…
$ decimalLongitude <dbl> 138.69976, 139.39130, 152.63339, -16.253…
$ issues <chr> "cdc,cdround", "cdc,cdround", "cdc,cdrou…
$ datasetKey <chr> "50c9509d-22c7-4a22-a47d-8c48425ef4a7", …
$ publishingOrgKey <chr> "28eb1a3f-1c15-4a95-931a-4af90ecb574d", …
$ installationKey <chr> "997448a8-f762-11e1-a439-00145eb45e9a", …
$ hostingOrganizationKey <chr> "28eb1a3f-1c15-4a95-931a-4af90ecb574d", …
$ publishingCountry <chr> "AU", "AU", "AU", "US", "US", "US", "US"…
$ protocol <chr> "DWC_ARCHIVE", "DWC_ARCHIVE", "DWC_ARCHI…
$ lastCrawled <chr> "2024-04-21T08:18:21.750+00:00", "2024-0…
$ lastParsed <chr> "2024-04-21T17:35:12.192+00:00", "2024-0…
$ crawlId <int> 451, 451, 451, 451, 451, 451, 451, 451, …
$ basisOfRecord <chr> "HUMAN_OBSERVATION", "HUMAN_OBSERVATION"…
$ occurrenceStatus <chr> "PRESENT", "PRESENT", "PRESENT", "PRESEN…
$ lifeStage <chr> "Larva", "Adult", "Larva", "Adult", "Adu…
$ taxonKey <int> 5133088, 5133088, 5133088, 5133088, 5133…
$ kingdomKey <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ phylumKey <int> 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, …
$ classKey <int> 216, 216, 216, 216, 216, 216, 216, 216, …
$ orderKey <int> 797, 797, 797, 797, 797, 797, 797, 797, …
$ familyKey <int> 7017, 7017, 7017, 7017, 7017, 7017, 7017…
$ genusKey <int> 5133087, 5133087, 5133087, 5133087, 5133…
$ speciesKey <int> 5133088, 5133088, 5133088, 5133088, 5133…
$ acceptedTaxonKey <int> 5133088, 5133088, 5133088, 5133088, 5133…
$ acceptedScientificName <chr> "Danaus plexippus (Linnaeus, 1758)", "Da…
$ kingdom <chr> "Animalia", "Animalia", "Animalia", "Ani…
$ phylum <chr> "Arthropoda", "Arthropoda", "Arthropoda"…
$ order <chr> "Lepidoptera", "Lepidoptera", "Lepidopte…
$ family <chr> "Nymphalidae", "Nymphalidae", "Nymphalid…
$ genus <chr> "Danaus", "Danaus", "Danaus", "Danaus", …
$ species <chr> "Danaus plexippus", "Danaus plexippus", …
$ genericName <chr> "Danaus", "Danaus", "Danaus", "Danaus", …
$ specificEpithet <chr> "plexippus", "plexippus", "plexippus", "…
$ taxonRank <chr> "SPECIES", "SPECIES", "SPECIES", "SPECIE…
$ taxonomicStatus <chr> "ACCEPTED", "ACCEPTED", "ACCEPTED", "ACC…
$ iucnRedListCategory <chr> "LC", "LC", "LC", "LC", "LC", "LC", "LC"…
$ dateIdentified <chr> "2024-01-01T01:12:40", "2024-01-01T04:19…
$ coordinateUncertaintyInMeters <dbl> 7, 3, 29845, 4, 5, NA, 11, NA, NA, NA, 4…
$ continent <chr> "OCEANIA", "OCEANIA", "OCEANIA", "AFRICA…
$ stateProvince <chr> "South Australia", "South Australia", "Q…
$ year <int> 2024, 2024, 2024, 2024, 2024, 2024, 2024…
$ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ eventDate <chr> "2024-01-01T11:41:07", "2024-01-01T11:11…
$ startDayOfYear <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ endDayOfYear <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ modified <chr> "2024-01-01T03:35:31.000+00:00", "2024-0…
$ lastInterpreted <chr> "2024-04-21T17:35:12.192+00:00", "2024-0…
$ references <chr> "https://www.inaturalist.org/observation…
$ license <chr> "http://creativecommons.org/licenses/by-…
$ isSequenced <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE…
$ isInCluster <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE…
$ datasetName <chr> "iNaturalist research-grade observations…
$ recordedBy <chr> "Graham Armstrong", "sarcastrophe", "Jac…
$ identifiedBy <chr> "Graham Armstrong", "sarcastrophe", "Jac…
$ geodeticDatum <chr> "WGS84", "WGS84", "WGS84", "WGS84", "WGS…
$ class <chr> "Insecta", "Insecta", "Insecta", "Insect…
$ countryCode <chr> "AU", "AU", "AU", "ES", "US", "US", "US"…
$ gbifRegion <chr> "OCEANIA", "OCEANIA", "OCEANIA", "EUROPE…
$ country <chr> "Australia", "Australia", "Australia", "…
$ publishedByGbifRegion <chr> "OCEANIA", "OCEANIA", "OCEANIA", "NORTH_…
$ rightsHolder <chr> "Graham Armstrong", "sarcastrophe", "Jac…
$ identifier <chr> "195422818", "195435992", "195447354", "…
$ `http://unknown.org/nick` <chr> "grahamarmstrong", "sarcastrophe", "arch…
$ verbatimEventDate <chr> "2024-01-01 11:41:07", "2024-01-01 11:11…
$ verbatimLocality <chr> "Woodforde SA 5072, Australia", "Pellari…
$ collectionCode <chr> "Observations", "Observations", "Observa…
$ gbifID <chr> "4507686934", "4507704024", "4507984043"…
$ occurrenceID <chr> "https://www.inaturalist.org/observation…
$ taxonID <chr> "48662", "48662", "48662", "48662", "486…
$ catalogNumber <chr> "195422818", "195435992", "195447354", "…
$ institutionCode <chr> "iNaturalist", "iNaturalist", "iNaturali…
$ eventTime <chr> "11:41:07+10:30", "11:11:32+10:30", "16:…
$ `http://unknown.org/captive` <chr> "wild", "wild", "wild", "wild", "wild", …
$ identificationID <chr> "439486498", "439524139", "439552350", "…
$ informationWithheld <chr> NA, NA, "Coordinate uncertainty increase…
$ occurrenceRemarks <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ infraspecificEpithet <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ sex <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ distanceFromCentroidInMeters <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ name <chr> "Danaus plexippus (Linnaeus, 1758)", "Da…
The package rnaturalearth provides a map of countries of the entire world.
ne_countries to pull country data and choose the scale (rnaturalearthhires is necessary for scale = “large”)First, let’s start with creating a base map of the world using ggplot2’s geom_sf().
Remember that sf geometries are no different than regular geometries, and can be displayed with the same level of control on their attributes:
We can layer the Monarch butterfly data from dan_ple_dat:
geom_point()world) with dataaes()
We can layer edit the points according to life stage (lifeStage) by specifying color in aes().
Note
Anything not mapped to the data can be put outside of aes()
plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library plotly.js.
Use the function ggplotly() to draw the graph with plotly.js.
Wrap your plot p in ggplotly():
Leaflet is an open-source JavaScript library for interactive maps.
The leaflet R package makes it easy to create Leaflet maps from R.
Create a base map with addTiles().
The different components of the map can be added using the pipe operator %>%.
Specify different tile options with addProviderTiles
We can do things like add markers and tooltips.
Add data when initializing the leaflet() function
~ tells leaflet to look at the dataSpecify a color scale for your polygons with fillColor.
We have current GDP in U.S. dollars in the world data:
Simple feature collection with 6 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -73.36621 ymin: -22.40205 xmax: 109.4449 ymax: 41.9062
Geodetic CRS: WGS 84
name gdp_md geometry
1 Zimbabwe 21440 MULTIPOLYGON (((31.28789 -2...
2 Zambia 23309 MULTIPOLYGON (((30.39609 -1...
3 Yemen 22581 MULTIPOLYGON (((53.08564 16...
4 Vietnam 261921 MULTIPOLYGON (((104.064 10....
5 Venezuela 482359 MULTIPOLYGON (((-60.82119 9...
6 Vatican -99 MULTIPOLYGON (((12.43916 41...
Specify a color palette and a domain:
Add the colors to your polygons by using pal and the variable you are using to fill the polygons (gdp_md).
~ tells leaflet to look at the dataAssignment 12 will walk you through creating a Leaflet map.
Final projects!